package cn.itcast.user.controller;

import cn.itcast.hmall.dto.common.ThreadLocalUtil;
import cn.itcast.hmall.pojo.user.Address;
import cn.itcast.user.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     * 通过用户id获得用户所有地址
     * @return
     */
    @GetMapping("/uid")
    public List<Address> addressById(){
        //从线程中获得当前用户id
        Long userId = ThreadLocalUtil.getUserId();
       return addressService.addressById(userId);
    }

    /**
     * 根据地址id获得地址
     * @param addressId
     * @return
     */
    @GetMapping("/{addressId}")
    public Address selectAddressById(@RequestParam("addressId") Long addressId){
       return addressService.selectAddressById(addressId);
    }
}
